import { createRouter, createWebHashHistory } from "vue-router";

import { staticRoutes } from "../utils/constData";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...staticRoutes],
});

// // 构建完整的树结构
// let flag = false;
// router.beforeEach(async (to, from, next) => {
//   console.log(from)
//   const { menu, role } = useMenuStore();
//   if (storage.get("token")) {
//     let res = await getmenu();
//     let { pagerole: ids, btnrole } = (await Igetrole() as any);
//     role.setv([...new Set(btnrole)]);
//     // 构建从根到叶子的路径
//     const menuTree = buildSubtreeFromLeaves(res.data, ids);
//     let ff = createApiRouter(menuTree) as any;
//     menu.setv(ff);
//     router.clearRoutes();
//     let item = staticRoutes.filter((es) => es.name == "layout")[0] as any;
//     for (let e of ff) {
//       item.children?.push(e);
//     }
//     for (let e of staticRoutes) {
//       router.addRoute(e);
//     }
//     if (!flag) {
//       flag = true;
//       return next({ ...to, replace: true });
//     }
//   }
//   next();
// });

export default router;
